package com.bootdo.contract.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.YutableDO;
import com.bootdo.contract.service.YutableService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-02 12:53:41
 */

@Controller
@RequestMapping ( "/contract/yutable" )
public class YutableController {
    @Autowired
    private YutableService yutableService;
    @Autowired
    UserService userService;

    @GetMapping ()
    @RequiresPermissions ( "contract:yutable:yutable" )
    String Yutable() {
        return "contract/yutable/yutable";
    }

    @ResponseBody
    @GetMapping ( "/list" )
    @RequiresPermissions ( "contract:yutable:yutable" )
    public PageUtils list(@RequestParam Map <String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List <YutableDO> yutableList = yutableService.list(query);
        int total = yutableService.count(query);
        PageUtils pageUtils = new PageUtils(yutableList, total);
        return pageUtils;
    }

    @GetMapping ( "/add" )
    @RequiresPermissions ( "contract:yutable:add" )
    String add(Model model) {
        return "contract/yutable/add";
    }

    @GetMapping ( "/addManager/{id}" )
    @RequiresPermissions ( "contract:yutable:add" )
    String addManager(@PathVariable ( "id" ) Integer id, Model model) {
        List <UserDO> userList = userService.list();
        model.addAttribute("userList", userList);
        model.addAttribute("id", id);
        return "contract/yutable/addManager";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping ( "/save" )
    @RequiresPermissions ( "contract:yutable:add" )
    public R save(@RequestParam ( "yuname" ) String yuname,
                  @RequestParam ( "explain" ) String explain,
                  @RequestParam ( "name" ) String name,
                  @RequestParam ( "filepath" ) String filepath) {
        YutableDO yutable = new YutableDO();
        yutable.setStafftname(yuname + "_Staff");
        yutable.setTestpapertname(yuname + "_Testpaper");
        yutable.setAchievementtname(yuname + "_Achievement");
        yutable.setCreatetime(new Date());
        yutable.setYuname(yuname);
        yutable.setName(name);
        yutable.setFilepath(filepath);
        yutable.setExplain(explain);
        yutableService.createStaff(yutable);
        yutableService.createTestpaper(yutable);
        yutableService.createAchievement(yutable);
        if (yutableService.save(yutable) > 0) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 删除
     */
    @PostMapping ( "/remove" )
    @ResponseBody
    @RequiresPermissions ( "contract:yutable:remove" )
    public R remove(Integer id) {
        Map <String, Object> map = new HashMap <>();
        map.put("id", id);
        List <YutableDO> list = yutableService.list(map);
        if (list.size() >= 1) {
            yutableService.deleteStaff(list.get(0));
            yutableService.deleteTestpaper(list.get(0));
            yutableService.deleteAchievement(list.get(0));
            if (yutableService.remove(id) > 0) {
                return R.ok();
            }
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping ( "/update" )
    @ResponseBody
    @RequiresPermissions ( "contract:yutable:edit" )
    public R update(Integer id, String manager) {
        YutableDO yutableDO = new YutableDO();
        yutableDO.setId(id);
        String[] split = manager.split(",");
        yutableDO.setManager(Integer.parseInt(split[0]));
        yutableDO.setManagername(split[1]);
        if (yutableService.update(yutableDO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping ( "/batchRemove" )
    @ResponseBody
    @RequiresPermissions ( "contract:yutable:batchRemove" )
    public R remove(@RequestParam ( "ids[]" ) Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Map <String, Object> map = new HashMap <>();
            map.put("id", ids[i]);
            List <YutableDO> list = yutableService.list(map);
            if (list.size() >= 1) {
                yutableService.deleteStaff(list.get(0));
                yutableService.deleteTestpaper(list.get(0));
                yutableService.deleteAchievement(list.get(0));
            }
        }
        yutableService.batchRemove(ids);
        return R.ok();
    }

}
